/*
 * sqlFunc.cpp
 *
 *  Created on: 18-Dec-2009
 *      Author: Atul Shukla
 */
#include"sqlFunc.h"
#define SPACE 10
struct te {
	string colname;
	syscols s;
};
systable syst(string table);
void _create(string table, vector<ckval> kv) {
	systable check = syst(table);

	if (check.tableDP == -1) {//table does not exist
		int tableDP = heap->createHeapFile();
		rid r;
		systable systab;
		systab.tableid = 1;
		strcpy(systab.tablename, table.c_str());
		systab.tableDP = tableDP;

		heap->insertRecord(SYSTABLE_DP, (unsigned char*) &systab,
				sizeof(systable), r);
		vector<ckval>::iterator it;
		syscols col;
		int pos = 0;
		for (it = kv.begin(); it != kv.end(); it++) {
			col.char_size = -1;
			int i;
			char *temp = (char*) (*it).def.c_str();
			for (i = 0; i < (MAX_VARCHAR - 1); i++) {
				if (temp[i] == '\0')
					break;
				col.colndefault[i] = temp[i];
			}
			col.colndefault[i] = '\0';// always end properly

			//col.colnid;
			temp = (char*) (*it).name.c_str();
			for (i = 0; i < (20 - 1); i++) {
				if (temp[i] == '\0')
					break;

				col.colnname[i] = temp[i];
			}
			col.colnname[i] = '\0';// always end properly
			col.isNULL = (*it).isnull;
			col.isdefault = (*it).isdefault;
			col.isindex = (*it).isindex;

			col.pposition = pos++;
			col.tableDP = tableDP;

			if (!strcasecmp((*it).type.c_str(), "INT"))
				col.type = 0;
			else if (!strcasecmp((*it).type.c_str(), "LONG"))
				col.type = 1;
			else if (!strcasecmp((*it).type.c_str(), "FLOAT"))
				col.type = 2;
			else if (!strcasecmp((*it).type.c_str(), "DATE"))
				col.type = 4;
			else if (!strcasecmp((*it).type.c_str(), "TIME"))
				col.type = 6;
			else {//check for char and varchar
				char* pt = (char*) strpbrk((*it).type.c_str(), "/");
				pt++;
				int i = atoi(pt);
				if ((*it).type.c_str()[0] == 'c' || (*it).type.c_str()[0]
						== 'C') {
					if (i > MAX_STRING) {
						cout << "Char max size" << MAX_STRING << "Exceeded";
						return;
					}
					col.type = 3;//char 3

				}
				if ((*it).type.c_str()[0] == 'v' || (*it).type.c_str()[0]
						== 'V') {
					if (i > MAX_VARCHAR) {
						cout << "Varchar max size" << MAX_STRING << "Exceeded";
						return;
					}
					col.type = 5;//varchar 5
				}
				col.char_size = i;

			}
			if (col.isindex) {

				sysindex ind;
				if (col.type != VARCHAR && col.type != STRING) {
					ind.keyformat[0] = col.type;

				} else {
					ind.keyformat[0] = col.type;
					ind.keyformat[1] = col.char_size;

				}
				int rootheadpge;

				BPlusTree *bt = new BPlusTree(-1, ind.keyformat, 1,
						&rootheadpge);
				ind.idxid = 1;
				ind.tableDP = tableDP;
				ind.noskeyattr = 1;
				ind.roothead = rootheadpge;
				ind.colms[0] = col.pposition;
//				cout << "\n index head " << rootheadpge;
				heap->insertRecord(SYSIN_DP, (unsigned char*) &ind,
						sizeof(sysindex), r);
			}
			heap->insertRecord(SYSCOL_DP, (unsigned char*) &col,
					sizeof(syscols), r);
		}
	} else {//table exists
		cout << "\nTable Already Exists";

	}
}

void _drop(vector<string> tablelist) {

}

systable syst(string table) {
	vector<record> resultset;
	heap->openScan(SYSTABLE_DP, resultset);
	vector<record>::iterator it;
	systable st;
	st.tableDP = -1;
	for (it = resultset.begin(); it != resultset.end(); ++it) {
		st = *((systable*) ((*it).rec));
		if (!strcmp(table.c_str(), st.tablename))
			break;
		st.tableDP = -1;
	}
	return st;
}

vector<systable> syst_all() {
	vector<record> resultset;
	vector<systable> rset;

	heap->openScan(SYSTABLE_DP, resultset);
	vector<record>::iterator it;
	for (it = resultset.begin(); it != resultset.end(); ++it) {
		systable st;
		st = *((systable*) ((*it).rec));
		rset.push_back(st);

	}
	return rset;
}

syscols sysc(int tableDP, string colname) {
	vector<record> resultset;
	heap->openScan(SYSCOL_DP, resultset);
	vector<record>::iterator it;
	syscols sc;
	sc.tableDP = -1;
	for (it = resultset.begin(); it != resultset.end(); ++it) {
		sc = *((syscols*) ((*it).rec));
		if (!strcmp(colname.c_str(), sc.colnname) && tableDP == sc.tableDP)
			break;
		sc.tableDP = -1;
	}

	return sc;
}

syscols sysc_pos(int tableDP, int pos) {
	vector<record> resultset;
	heap->openScan(SYSCOL_DP, resultset);
	vector<record>::iterator it;
	syscols sc;
	sc.tableDP = -1;
	for (it = resultset.begin(); it != resultset.end(); ++it) {
		sc = *((syscols*) ((*it).rec));
		if (pos == sc.pposition && tableDP == sc.tableDP)
			break;
		sc.tableDP = -1;
	}

	return sc;
}
vector<te> sysc_all(int tableDP) {
	vector<te> col_struct_map;
	vector<record> resultset;
	heap->openScan(SYSCOL_DP, resultset);
	vector<record>::iterator it;
	for (it = resultset.begin(); it != resultset.end(); ++it) {
		te temp;
		temp.s = *((syscols*) ((*it).rec));
		if (tableDP == temp.s.tableDP) {
			temp.colname = temp.s.colnname;
			col_struct_map.push_back(temp);
		}
	}
	return col_struct_map;
}

vector<sysindex> sysi(int tableDP) {
	vector<record> resultset;
	vector<sysindex> in;
	heap->openScan(SYSIN_DP, resultset);
	vector<record>::iterator it;

	for (it = resultset.begin(); it != resultset.end(); ++it) {
		sysindex sc = *((sysindex*) ((*it).rec));
		if (tableDP == sc.tableDP)
			in.push_back(sc);

	}

	return in;
}

sysindex sysi_on_col(vector<sysindex> in, int col) {

	sysindex ret;
	ret.tableDP = -1;
	vector<sysindex>::iterator it;

	for (it = in.begin(); it != in.end(); ++it) {
		if ((*it).colms[0] == col) {
			ret = *it;
			break;
		}
	}
	return ret;
}

vector<sysindex*> sysi_all() {
	vector<record> resultset;
	vector<sysindex*> rset;

	heap->openScan(SYSIN_DP, resultset);
	vector<record>::iterator it;
	for (it = resultset.begin(); it != resultset.end(); ++it) {
		sysindex* st;
		st = (sysindex*) ((*it).rec);
		rset.push_back(st);

	}
	return rset;
}
int keyComp(unsigned char* key1, unsigned char* key2, int type, int size = 0) {//values positive key1 is bigger
	int k = 0;
	int j = 0;
	switch (type) {
	case INTEGER:
		//		cout << "\nKEY111  " << (*((int*) (key1 + j))) << " KEY222  "
		//				<< *((int*) (key2 + j));
		if (*((int*) (key1 + j)) != *((int*) (key2 + j))) {

			return *((int*) (key1 + j)) - *((int*) (key2 + j));
		}

		break;

	case LONG:
		if (*((long*) (key1 + j)) != *((long*) (key2 + j))) {
			return *((long*) (key1 + j)) - *((long*) (key2 + j));
		}

		break;

	case FLOAT:
		if (*((float*) (key1 + j)) != *((float*) (key2 + j))) {
			return *((float*) (key1 + j)) - *((float*) (key2 + j));
		}

		break;
	case STRING:
	case VARCHAR:
	case TIME:
	case DATE: {
		for (int m = 0; m < size; m++) {
			if (*((key1 + j + m)) != *((key2 + j + m))) {
				return *((key1 + j + m)) - *((key2 + j + m));
			}
		}

		break;
	}

	}

	return 0;
}
int postFix(vector<int> &expr) {
	stack<int> st;
	vector<int>::iterator it;
	for (it = expr.begin(); it != expr.end(); ++it) {
		if (*it >= 0) {
			st.push(*it);
		} else {
			int a, b;
			a = st.top();
			st.pop();
			b = st.top();
			st.pop();
			if (*it == -1) { // and
				st.push(a * b);
			} else if (*it == -2) {
				st.push(a + b);
			}
		}

	}
	return st.top();

}
bool isRecord(record &r, int tableDP, vector<int> condition,
		vector<string> left, vector<string> op, vector<string> right, Status &s) {
	vector<int> truth;
	vector<string>::iterator a, b, c;
	for (a = left.begin(), b = op.begin(), c = right.begin(); a != left.end(), b
			!= op.end(), c != right.end(); ++a, ++b, ++c) {

		//		cout << endl << *a << " " << *b << " " << *c;

		syscols it = sysc(tableDP, *a);
		if (it.tableDP == -1) {
			cout << "Column Not Found::" << *a;
			s = error;
			return false;
		}
		unsigned char* rec = r.rec;

		string temp;
		static int pos;
		int t;
		int length;
		unsigned char* iint;
		unsigned char* lng;
		unsigned char* flot;
		unsigned char* str;
		unsigned char* date;
		unsigned char* time;
		unsigned char* varchar;
		int nextpos, zeropos;

		pos = (it.pposition) * sizeof(int);
		t = *((int*) &rec[pos]);//current pos

		nextpos = pos + sizeof(int);
		zeropos = *(int*) &rec[0];
		if (nextpos == zeropos) {
			length = r.recLen - t;//next position - current
		} else {
			length = (*(int*) &rec[nextpos]) - t;//next position - current
		}
		if (length == 0) { // for null;
			s = ok;
			return false;

		} else {
			unsigned char* te;
			int keyCompRet;
			int tempa;
			switch (it.type) {
			case INTEGER: {
				int temp = *((int*) &rec[t]);// innt[4];
				iint = (unsigned char*) &temp;

				//				iint = (unsigned char*) &rec[t];
				tempa = atoi((*c).c_str());
				te = (unsigned char*) &(tempa);
				//				cout<<*((int*)(iint))<<" bhai bhai "<<*((int*)(te));
				keyCompRet = keyComp(iint, te, it.type);
				//cout << " " << keyCompRet << "\n";

				break;
			}
			case LONG:

				lng = (unsigned char*) &rec[t];
				te = (unsigned char*) ((*c).c_str());
				keyCompRet = keyComp(lng, te, it.type);
				break;
			case FLOAT:

				flot = (unsigned char*) &rec[t];
				te = (unsigned char*) ((*c).c_str());
				keyCompRet = keyComp(flot, te, it.type);
				break;
			case STRING:

				//				str = &rec[t];
				//				cout << "\nIn Select::";
				//				for (int i = 0; i < it.char_size; i++) {
				//					if (str[i] == '\0')
				//						break;
				//					cout << str[i];
				//				}
				str = (unsigned char*) &rec[t];
				te = (unsigned char*) ((*c).c_str());
				keyCompRet = keyComp(str, te, it.type, it.char_size);

				break;
			case DATE:

				date = &rec[t];
				te = (unsigned char*) ((*c).c_str());
				keyCompRet = keyComp(date, te, it.type, DATE_SIZE);
				break;

			case VARCHAR:
				nextpos = pos + sizeof(int);
				zeropos = *(int*) &rec[0];
				if (nextpos == zeropos) {
					length = r.recLen - t;//next position - current
				} else {
					length = (*(int*) &rec[nextpos]) - t;//next position - current
				}
				varchar = &rec[t];
				te = (unsigned char*) ((*c).c_str());
				keyCompRet = keyComp(varchar, te, it.type, length);
				break;
			case TIME:

				time = &rec[t];
				te = (unsigned char*) ((*c).c_str());
				keyCompRet = keyComp(time, te, it.type, TIME_SIZE);
				break;

			}//end of switch
			if (!strcmp((*b).c_str(), "<")) {

				truth.push_back(keyCompRet < 0 ? 1 : 0);

			} else if (!strcmp((*b).c_str(), "<=")) {

				truth.push_back((keyCompRet <= 0 ? 1 : 0));

			} else if (!strcmp((*b).c_str(), ">")) {

				truth.push_back(keyCompRet > 0 ? 1 : 0);

			} else if (!strcmp((*b).c_str(), ">=")) {

				truth.push_back(keyCompRet >= 0 ? 1 : 0);

			} else if (!strcmp((*b).c_str(), "!=")) {

				truth.push_back(keyCompRet != 0 ? 1 : 0);

			} else if (!strcmp((*b).c_str(), "==")) {

				truth.push_back(keyCompRet == 0 ? 1 : 0);

			}
		}
	}//end of for

	vector<int>::iterator ite;

	for (int i = 0; i < condition.size(); i++)
		if (condition[i] >= 0)
			condition[i] = truth[condition[i]];

	return postFix(condition);
}
void readRecord(record &r, vector<syscols> &col_struct_list) {
	unsigned char* rec = r.rec;
	int pos;
	int t;
	int length;
	int iint;
	long lng;
	float flot;
	unsigned char* str;
	unsigned char* date;
	unsigned char* varchar;
	unsigned char* time;

	int nextpos, zeropos;
	vector<syscols>::iterator it;
	cout << "\n";

	for (it = col_struct_list.begin(); it != col_struct_list.end(); ++it) {
		pos = ((*it).pposition) * sizeof(int);
		t = *((int*) &rec[pos]);//current pos

		nextpos = pos + sizeof(int);
		zeropos = *(int*) &rec[0];
		if (nextpos == zeropos) {
			length = r.recLen - t;//next position - current
		} else {
			length = (*(int*) &rec[nextpos]) - t;//next position - current
		}
		if (length == 0) {
			cout << setw(SPACE) << left << "NULL";

		} else {
			string temp;
			temp.clear();
			switch ((*it).type) {
			case 0:

				iint = *(int*) &rec[t];
				cout << setw(SPACE) << left << iint;
				break;
			case 1:

				lng = *(long*) &rec[t];
				cout << setw(SPACE) << left << lng;
				break;
			case 2:

				flot = *(float*) &rec[t];
				cout << setw(SPACE) << left << flot;
				break;
			case 3:

				str = &rec[t];

				for (int i = 0; i < (*it).char_size; i++) {
					if (str[i] == '\0')
						break;

					temp.push_back(str[i]);
				}
				temp.push_back('\0');
				cout << setw((*it).char_size + 1) << left << temp;
				break;
			case 4:

				date = &rec[t];

				for (int i = 0; i < DATE_SIZE; i++) {

					temp.push_back(date[i]);
				}
				temp.push_back('\0');
				cout << setw(SPACE + 1) << left << temp;

				break;

			case 5:
				nextpos = pos + sizeof(int);
				zeropos = *(int*) &rec[0];
				if (nextpos == zeropos) {
					length = r.recLen - t;//next position - current
				} else {
					length = (*(int*) &rec[nextpos]) - t;//next position - current
				}
				varchar = &rec[t];

				for (int i = 0; i < length; i++) {

					temp.push_back(varchar[i]);
				}
				temp.push_back('\0');
				cout << setw((*it).char_size + 1) << left << temp;
				break;
			case 6:

				time = &rec[t];

				for (int i = 0; i < TIME_SIZE; i++) {

					temp.push_back(time[i]);

				}
				temp.push_back('\0');

				cout << setw(SPACE + 1) << left << temp;
				break;

			}//end of switch
			cout << " ";
			cout.flush();

		}
	}

}

void _select(string table, vector<string> columnlist, vector<int> condition,
		vector<string> l, vector<string> op, vector<string> r) {
	Timer timer;
	int cnt = 0;
	timer.tapTime();
	vector<int>::iterator il;
	vector<string>::iterator a, b, c;

	cout.flush();
	if (!strcasecmp(table.c_str(), "systable")) {
		vector<systable> rset = syst_all();
		vector<systable>::iterator it;
		cout << "Table DP		Table Name		";
		for (it = rset.begin(); it != rset.end(); ++it) {
			cout << "\n" << (*it).tableDP << "\t";
			cout <<(*it).tablename << "\t";
		}
	} else if (!strcasecmp(table.c_str(), "syscolumn")) {

		//		vector<te> col_struct_map=sysc_all();
		//		vector<te>::iterator it;


		vector<systable> rset = syst_all();
				vector<systable>::iterator bt;



				cout << setw(20) << left << "ColName";
				cout << setw(10) << left << "Type";
				cout << setw(10) << left << "IsNull";
				cout << setw(10) << left << "IsIndex";
				cout << setw(10) << left << "IsDef";
				cout << setw(10) << left << "DefVal";
				cout << setw(10) << left << "TableName";
				cout << setw(10) << left << "ColumnPosition\n";
				cout << setfill('-') << left << setw(100) << "-";

				cout << setfill(' ') << setw(1) << " ";
				for (bt = rset.begin(); bt != rset.end(); ++bt) {
					vector<te> rset = sysc_all((*bt).tableDP);
					vector<te>::iterator tt;

				for (tt = rset.begin(); tt != rset.end(); ++tt) {
syscols* it=&((*tt).s);
					cout << "\n";
					cout << setw(20) << left << (*it).colnname;

//					int temp = (*it).type;
//							switch ((int) (*it).type) {
//							case INTEGER:
//								cout << INTEGER;
//								break;
//							case LONG:
//								cout << LONG;
//								break;
//							case FLOAT:
//								cout << FLOAT;
//								break;
//							case STRING:
//								flag = true;
//								cout << STRING;
//								temp++;
//								break;
//							case VARCHAR:
//								flag = true;
//								cout << VARCHAR;
//								temp++;
//								break;
//
//							}
//					}
//
					cout << setw(10) << left << (*it).type;
					cout << setw(10) << left << (*it).isNULL;
					cout << setw(10) << left << (*it).isindex;
					cout << setw(10) << left << (*it).isdefault;
					cout << setw(10) << left << (*it).colndefault;
					cout << setw(10) << left << (*bt).tablename;
					cout << setw(10) << left << (*it).pposition;

				}
				}



	} else if (!strcasecmp(table.c_str(), "sysindex")) {
		vector<sysindex*> rset = sysi_all();
		vector<sysindex*>::iterator it;
		cout << setw(10) << left << "Index ID ";
		cout << setw(20) << left << "Keyformat ";
		cout << setw(10) << left << "NoKeyAttr ";
		cout << setw(10) << left << "RootHead ";
		cout << setw(10) << left << "TableDP \n";
		cout << setfill('-') << left << setw(50) << "-";

		cout << setfill(' ') << setw(1) << " ";
		for (it = rset.begin(); it != rset.end(); ++it) {

			cout << "\n";
			cout << setw(10) << left << (*it)->idxid;
			bool flag = false;
			int temp = (*it)->noskeyattr;
			for (int i = 0; i < temp; i++) {

				if (flag) {
					flag = false;
					cout << (int) (*it)->keyformat[i];
					continue;
				} else
					switch ((int) (*it)->keyformat[i]) {
					case INTEGER:
						cout << INTEGER;
						break;
					case LONG:
						cout << LONG;
						break;
					case FLOAT:
						cout << FLOAT;
						break;
					case STRING:
						flag = true;
						cout << STRING;
						temp++;
						break;
					case VARCHAR:
						flag = true;
						cout << VARCHAR;
						temp++;
						break;

					}
			}
			for (int i = temp; i < 19; i++)
				cout << " ";
			cout << setw(10) << left << (*it)->noskeyattr;
			cout << setw(10) << left << (*it)->roothead;
			cout << setw(10) << left << (*it)->tableDP;

		}

	} else {
		systable st;
		vector<syscols> col_struct_list;
		st = syst(table);
		if (st.tableDP == -1) { //not a valid Table
			cout << "\nTable not Found::" << table;
			return;
		}
		string temp;
		vector<string>::iterator it;
		for (it = columnlist.begin(); it != columnlist.end(); ++it) {
			temp = *it;
			if (!(strcmp(temp.c_str(), "*"))) {//equal to *
				vector<te> allcollist = sysc_all(st.tableDP);
				vector<te>::iterator rrr;
				for (rrr = allcollist.begin(); rrr != allcollist.end(); ++rrr) {
					col_struct_list.push_back((*rrr).s);
				}
			} else {

				syscols sc;
				sc = sysc(st.tableDP, temp);
				if (sc.tableDP == -1) { //not a valid column
					cout << "\nColumn not Found::" << temp;
					return;
				}
				col_struct_list.push_back(sc);
			}
		}

		vector<syscols>::iterator ti;
		for (ti = col_struct_list.begin(); ti != col_struct_list.end(); ++ti) {
			if ((*ti).type == VARCHAR || (*ti).type == STRING) {
				cout << setw((*ti).char_size) << left << (*ti).colnname;
				cout << " ";
			} else {
				cout << setw(SPACE) << left << (*ti).colnname;
				cout << " ";
			}

		}
		cout << "\n";
		for (ti = col_struct_list.begin(); ti != col_struct_list.end(); ++ti) {
			if ((*ti).type == VARCHAR || (*ti).type == STRING) {
				cout << setfill('-') << setw((*ti).char_size) << "-";
				cout << " ";
			} else {
				cout << setfill('-') << setw(SPACE) << left << '-';
				cout << " ";
			}
		}
		cout << setfill(' ') << setw(1) << " ";

		vector<record> resultset;

		vector<record>::iterator it1;
		bool isOpenScan = true;
		if (condition.size() == 1) {//look for index
			isOpenScan = false;
			vector<string>::iterator lit;
			vector<string>::iterator rit;
			vector<string>::iterator opit;
			vector<sysindex> sysind;
			sysind = sysi(st.tableDP);

			for (lit = l.begin(), opit = op.begin(), rit = r.begin(); lit
					!= l.end(), opit != op.end(), rit != r.end(); ++lit, ++rit, ++opit) {
				syscols litsyscol = sysc(st.tableDP, *lit);
				sysindex colindx = sysi_on_col(sysind, litsyscol.pposition);

				/////////////index is there///////////////////

				if (colindx.tableDP != -1 && strcmp((*opit).c_str(), "!=")) {
					vector<BPlusTree::searchresultstruct> indresult;
					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					//					cout << "search value " << (*rit) << endl;

					int retcount;
					int dd0;
					long dd1;
					float dd2;

					switch (litsyscol.type) {
					case INTEGER: {
						dd0 = atoi((*rit).c_str());

						if (!strcmp((*opit).c_str(), "<")) {

							retcount = bpt->rangeSearch(NULL,
									(unsigned char*) (&dd0), &indresult,
									EXCLUDE, EXCLUDE);

						} else if (!strcmp((*opit).c_str(), "<=")) {
							retcount = bpt->rangeSearch(NULL,
									(unsigned char*) (&dd0), &indresult,
									INCLUDE, INCLUDE);

						} else if (!strcmp((*opit).c_str(), ">")) {
							retcount = bpt->rangeSearch(
									(unsigned char*) (&dd0), NULL, &indresult,
									EXCLUDE, EXCLUDE);

						} else if (!strcmp((*opit).c_str(), ">=")) {
							retcount = bpt->rangeSearch(
									(unsigned char*) (&dd0), NULL, &indresult,
									INCLUDE, INCLUDE);

						} else if (!strcmp((*opit).c_str(), "==")) {

							retcount = bpt->rangeSearch(
									(unsigned char*) (&dd0), NULL, &indresult,
									EQUAL, EQUAL);

						}

						break;
					}
					case LONG:
						dd1 = atol((*rit).c_str());
						if (!strcmp((*opit).c_str(), "<")) {

							retcount = bpt->rangeSearch(NULL,
									(unsigned char*) (&dd1), &indresult,
									EXCLUDE, EXCLUDE);

						} else if (!strcmp((*opit).c_str(), "<=")) {
							retcount = bpt->rangeSearch(NULL,
									(unsigned char*) (&dd1), &indresult,
									INCLUDE, INCLUDE);

						} else if (!strcmp((*opit).c_str(), ">")) {
							retcount = bpt->rangeSearch(
									(unsigned char*) (&dd1), NULL, &indresult,
									EXCLUDE, EXCLUDE);

						} else if (!strcmp((*opit).c_str(), ">=")) {
							retcount = bpt->rangeSearch(
									(unsigned char*) (&dd1), NULL, &indresult,
									INCLUDE, INCLUDE);

						} else if (!strcmp((*opit).c_str(), "==")) {

							retcount = bpt->rangeSearch(
									(unsigned char*) (&dd1), NULL, &indresult,
									EQUAL, EQUAL);

						}

						break;
					case FLOAT:
						dd2 = atof((*rit).c_str());
						if (!strcmp((*opit).c_str(), "<")) {

							retcount = bpt->rangeSearch(NULL,
									(unsigned char*) (&dd2), &indresult,
									EXCLUDE, EXCLUDE);

						} else if (!strcmp((*opit).c_str(), "<=")) {
							retcount = bpt->rangeSearch(NULL,
									(unsigned char*) (&dd2), &indresult,
									INCLUDE, INCLUDE);

						} else if (!strcmp((*opit).c_str(), ">")) {
							retcount = bpt->rangeSearch(
									(unsigned char*) (&dd2), NULL, &indresult,
									EXCLUDE, EXCLUDE);

						} else if (!strcmp((*opit).c_str(), ">=")) {
							retcount = bpt->rangeSearch(
									(unsigned char*) (&dd2), NULL, &indresult,
									INCLUDE, INCLUDE);

						} else if (!strcmp((*opit).c_str(), "==")) {

							retcount = bpt->rangeSearch(
									(unsigned char*) (&dd2), NULL, &indresult,
									EQUAL, EQUAL);

						}

						break;
					case STRING:
					case VARCHAR: {
						char maxchar[litsyscol.char_size];
						strcpy(maxchar, (*rit).c_str());
						for (int i = strlen((*rit).c_str()); i
								< litsyscol.char_size; i++) {
							maxchar[i] = '\0';
						}

						if (!strcmp((*opit).c_str(), "<")) {

							retcount = bpt->rangeSearch(NULL,
									(unsigned char*) (maxchar), &indresult,
									EXCLUDE, EXCLUDE);

						} else if (!strcmp((*opit).c_str(), "<=")) {
							retcount = bpt->rangeSearch(NULL,
									(unsigned char*) (maxchar), &indresult,
									INCLUDE, INCLUDE);

						} else if (!strcmp((*opit).c_str(), ">")) {
							retcount = bpt->rangeSearch(
									(unsigned char*) (maxchar), NULL,
									&indresult, EXCLUDE, EXCLUDE);

						} else if (!strcmp((*opit).c_str(), ">=")) {
							retcount = bpt->rangeSearch(
									(unsigned char*) (maxchar), NULL,
									&indresult, INCLUDE, INCLUDE);

						} else if (!strcmp((*opit).c_str(), "==")) {

							retcount = bpt->rangeSearch(
									(unsigned char*) (maxchar), NULL,
									&indresult, EQUAL, EQUAL);

						}
					}
						break;
					}//end of switch

					//					cout << "retcount " << retcount << endl;
					vector<BPlusTree::searchresultstruct>::iterator bit;
					int len;
					for (bit = indresult.begin(); bit != indresult.end(); ++bit) {

						record rec;
						rec.recLen = len;
						heap->getRecord(st.tableDP, (*bit).record, rec.rec, len);
						rec.recLen = len;
						rec.rid = (*bit).record;
						//cout << rec.rid.pageno << "\t";
						resultset.push_back(rec);

					}
				} else {
					isOpenScan = true;
					resultset.clear();
					break;
				}
			}

		}

		if (isOpenScan == true) {
			resultset.clear();
			heap->openScan(st.tableDP, resultset);
		}
		//int cnt=0;
		for (it1 = resultset.begin(); it1 != resultset.end(); ++it1) {
			Status stu = ok;
			bool truth = 1;
			if (condition.size() != 0) {

				truth = isRecord(*it1, st.tableDP, condition, l, op, r, stu);

			}
			if (stu == ok && truth) {
				cnt++;
				readRecord(*it1, col_struct_list);
			}
			//where condition due
		}
	}
	cout << "\nNos Record::" << cnt << "\nTime Taken::" << timer.unTapTime();
}
void updateRecord(record &r, map<string, string> &columnlist, int tableDP) {

	unsigned char* rec = r.rec;
	int pos;
	int t;
	int length;
	int* iint;
	long* lng;
	float* flot;
	unsigned char* str;
	unsigned char* date;
	unsigned char* varchar;
	unsigned char* time;

	int nextpos, zeropos;
	map<string, string>::iterator ti;
	cout << "\n";
	syscols it;
	for (ti = columnlist.begin(); ti != columnlist.end(); ++ti) {
		it = sysc(tableDP, (*ti).first);
		pos = ((it).pposition) * sizeof(int);
		t = *((int*) &rec[pos]);//current pos

		nextpos = pos + sizeof(int);
		zeropos = *(int*) &rec[0];
		if (nextpos == zeropos) {
			length = r.recLen - t;//next position - current
		} else {
			length = (*(int*) &rec[nextpos]) - t;//next position - current
		}
		if (length == 0) {
			cout << setw(SPACE) << left << "NULL";

		} else {

			vector<sysindex> sysind = sysi(tableDP);

			syscols litsyscol = sysc(tableDP, (*ti).first);
			sysindex colindx = sysi_on_col(sysind, litsyscol.pposition);

			string temp;
			temp.clear();
			switch ((it).type) {
			case 0: {
				unsigned char* old;
				unsigned char* neew;

				old = &rec[t];

				int temp = atoi((*ti).second.c_str());
				neew = (unsigned char*) (&temp);

				if (colindx.tableDP != -1) {//index is on coloumn;
					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(old, r.rid);
					bpt->insert(neew, r.rid);
				}
				iint = (int*) &rec[t];
				*iint = atoi((*ti).second.c_str());
				break;
			}
			case 1: {
				unsigned char* old;
				unsigned char* neew;

				old = &rec[t];
				long temp = atol((*ti).second.c_str());
				neew = (unsigned char*) (&temp);

				if (colindx.tableDP != -1) {//index is on coloumn;
					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(old, r.rid);
					bpt->insert(neew, r.rid);
				}
				lng = (long*) &rec[t];
				*lng = atol((*ti).second.c_str());
				break;
			}
			case 2: {
				unsigned char* old;
				unsigned char* neew;

				old = &rec[t];
				float temp = atof((*ti).second.c_str());
				neew = (unsigned char*) (&temp);

				if (colindx.tableDP != -1) {//index is on coloumn;
					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(old, r.rid);
					bpt->insert(neew, r.rid);
				}
				flot = (float*) &rec[t];
				*flot = atof((*ti).second.c_str());
				break;
			}
			case 3: {
				unsigned char* old;
				unsigned char* neew;

				old = &rec[t];
				neew = (unsigned char*) ((*ti).second.c_str());

				if (colindx.tableDP != -1) {//index is on coloumn;
					unsigned char oldmaxchar[it.char_size];

					for (int i = 0; i < it.char_size; i++) {
						if (old[i] != '\0')
							oldmaxchar[i] = old[i];
						else
							oldmaxchar[i] = '\0';
					}
					unsigned char newmaxchar[it.char_size];

					for (int i = 0; i < it.char_size; i++) {
						if (str[i] != '\0')
							newmaxchar[i] = neew[i];
						else
							newmaxchar[i] = '\0';
					}

					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(oldmaxchar, r.rid);
					bpt->insert(newmaxchar, r.rid);
				}



				for (int i = 0; i < (it).char_size; i++) {
					if (((*ti).second.c_str())[i] != '\0')
						str[i] = (((*ti).second).c_str())[i];
					else
						str[i] = '\0';

				}

				break;
			}
			case 4: {
				unsigned char* old;
				unsigned char* neew;

				old = &rec[t];
				neew = (unsigned char*) ((*ti).second.c_str());
				if (colindx.tableDP != -1) {//index is on coloumn;
					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(old, r.rid);
					bpt->insert(neew, r.rid);
				}

				date = &rec[t];

				for (int i = 0; i < DATE_SIZE; i++) {

					date[i] = (((*ti).second).c_str())[i];
				}
				break;
			}
			case 5: {
				nextpos = pos + sizeof(int);
				zeropos = *(int*) &rec[0];
				if (nextpos == zeropos) {
					length = r.recLen - t;//next position - current
				} else {
					length = (*(int*) &rec[nextpos]) - t;//next position - current
				}

				unsigned char* old;
				unsigned char* neew;

				old = &rec[t];
				neew = (unsigned char*) ((*ti).second.c_str());

				if (colindx.tableDP != -1) {//index is on coloumn;
					unsigned char oldmaxchar[it.char_size];

					for (int i = 0; i < it.char_size; i++) {
						if (old[i] != '\0')
							oldmaxchar[i] = old[i];
						else
							oldmaxchar[i] = '\0';
					}
					unsigned char newmaxchar[it.char_size];

					for (int i = 0; i < it.char_size; i++) {
						if (str[i] != '\0')
							newmaxchar[i] = neew[i];
						else
							newmaxchar[i] = '\0';
					}

					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(oldmaxchar, r.rid);
					bpt->insert(newmaxchar, r.rid);
				}

				//				varchar = &rec[t];
				int newVarcharLength = strlen(((*ti).second).c_str());
				int recLengthDiff = newVarcharLength - length;//positive if new is greater than old varchar

				unsigned char* rcord = (unsigned char*) malloc(r.recLen
						+ recLengthDiff);
				//copy old one till varchar starts
				for (int i = 0; i < t; i++) {
					rcord[i] = r.rec[i];
				}
				varchar = &rcord[t];
				for (int i = 0; i < strlen(((*ti).second).c_str()); i++) {

					varchar[i] = (((*ti).second).c_str())[i];
				}
				//copy old one which are remaning after varchar ends
				for (int i = t + newVarcharLength, j = t + length; j < r.recLen; j++, i++) {
					rcord[i] = rec[j];
				}

				int poszero = *(int*) (rcord);
				for (int i = litsyscol.pposition + 1; i * sizeof(int)
						!= poszero; i++) {
					int * handle = (int*) (rcord + i * sizeof(int));
					*handle = *handle + recLengthDiff;
				}

				//update for heap;
				r.rec = rcord;
				r.recLen = r.recLen + recLengthDiff;

				//				vector<te> tj=sysc_all(tableDP);
				//				vector<te>::iterator gt;
				//				for(gt=tj.begin();gt!=tj.end();gt++){
				//
				//					(*gt).s.pposition
				//				}
				break;
			}
			case 6: {
				unsigned char* old;
				unsigned char* neew;

				old = &rec[t];
				neew = (unsigned char*) ((*ti).second.c_str());

				if (colindx.tableDP != -1) {//index is on coloumn;
					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(old, r.rid);
					bpt->insert(neew, r.rid);
				}
				time = &rec[t];

				for (int i = 0; i < TIME_SIZE; i++) {

					time[i] = (((*ti).second).c_str())[i];

				}

				break;
			}

			}//end of switch
			cout << " ";
			cout.flush();

		}
	}

}
void _update(string table, map<string, string> columnlist,
		vector<int> condition, vector<string> l, vector<string> op, vector<
				string> r) {
	Timer timer;
	int cnt = 0;
	timer.tapTime();
	vector<int>::iterator il;
	vector<string>::iterator a, b, c;

	cout.flush();

	systable st;
	vector<syscols> col_struct_list;
	st = syst(table);
	if (st.tableDP == -1) { //not a valid Table
		cout << "\nTable not Found::" << table;
		return;
	}
	string temp;
	map<string, string>::iterator it;
	for (it = columnlist.begin(); it != columnlist.end(); ++it) {
		temp = (*it).first;
		syscols sc;
		sc = sysc(st.tableDP, temp);
		if (sc.tableDP == -1) { //not a valid column
			cout << "\nColumn not Found::" << temp;
			return;
		}
		col_struct_list.push_back(sc);

	}

	vector<record> resultset;

	vector<record>::iterator it1;
	bool isOpenScan = true;
	if (condition.size() == 1) {//look for index
		isOpenScan = false;
		vector<string>::iterator lit;
		vector<string>::iterator rit;
		vector<string>::iterator opit;
		vector<sysindex> sysind;
		sysind = sysi(st.tableDP);

		for (lit = l.begin(), opit = op.begin(), rit = r.begin(); lit
				!= l.end(), opit != op.end(), rit != r.end(); ++lit, ++rit, ++opit) {
			syscols litsyscol = sysc(st.tableDP, *lit);
			sysindex colindx = sysi_on_col(sysind, litsyscol.pposition);

			/////////////index is there///////////////////

			if (colindx.tableDP != -1 && strcmp((*opit).c_str(), "!=")) {
				vector<BPlusTree::searchresultstruct> indresult;
				BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
						colindx.noskeyattr, colindx.roothead);
				//					cout << "search value " << (*rit) << endl;

				int retcount;
				int dd0;
				long dd1;
				float dd2;

				switch (litsyscol.type) {
				case INTEGER: {
					dd0 = atoi((*rit).c_str());

					if (!strcmp((*opit).c_str(), "<")) {

						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd0), &indresult, EXCLUDE,
								EXCLUDE);

					} else if (!strcmp((*opit).c_str(), "<=")) {
						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd0), &indresult, INCLUDE,
								INCLUDE);

					} else if (!strcmp((*opit).c_str(), ">")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd0),
								NULL, &indresult, EXCLUDE, EXCLUDE);

					} else if (!strcmp((*opit).c_str(), ">=")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd0),
								NULL, &indresult, INCLUDE, INCLUDE);

					} else if (!strcmp((*opit).c_str(), "==")) {

						retcount = bpt->rangeSearch((unsigned char*) (&dd0),
								NULL, &indresult, EQUAL, EQUAL);

					}

					break;
				}
				case LONG:
					dd1 = atol((*rit).c_str());
					if (!strcmp((*opit).c_str(), "<")) {

						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd1), &indresult, EXCLUDE,
								EXCLUDE);

					} else if (!strcmp((*opit).c_str(), "<=")) {
						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd1), &indresult, INCLUDE,
								INCLUDE);

					} else if (!strcmp((*opit).c_str(), ">")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd1),
								NULL, &indresult, EXCLUDE, EXCLUDE);

					} else if (!strcmp((*opit).c_str(), ">=")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd1),
								NULL, &indresult, INCLUDE, INCLUDE);

					} else if (!strcmp((*opit).c_str(), "==")) {

						retcount = bpt->rangeSearch((unsigned char*) (&dd1),
								NULL, &indresult, EQUAL, EQUAL);

					}

					break;
				case FLOAT:
					dd2 = atof((*rit).c_str());
					if (!strcmp((*opit).c_str(), "<")) {

						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd2), &indresult, EXCLUDE,
								EXCLUDE);

					} else if (!strcmp((*opit).c_str(), "<=")) {
						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd2), &indresult, INCLUDE,
								INCLUDE);

					} else if (!strcmp((*opit).c_str(), ">")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd2),
								NULL, &indresult, EXCLUDE, EXCLUDE);

					} else if (!strcmp((*opit).c_str(), ">=")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd2),
								NULL, &indresult, INCLUDE, INCLUDE);

					} else if (!strcmp((*opit).c_str(), "==")) {

						retcount = bpt->rangeSearch((unsigned char*) (&dd2),
								NULL, &indresult, EQUAL, EQUAL);

					}

					break;
				case STRING:
				case VARCHAR: {
					char maxchar[litsyscol.char_size];
					strcpy(maxchar, (*rit).c_str());
					for (int i = strlen((*rit).c_str()); i
							< litsyscol.char_size; i++) {
						maxchar[i] = '\0';
					}

					if (!strcmp((*opit).c_str(), "<")) {

						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (maxchar), &indresult,
								EXCLUDE, EXCLUDE);

					} else if (!strcmp((*opit).c_str(), "<=")) {
						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (maxchar), &indresult,
								INCLUDE, INCLUDE);

					} else if (!strcmp((*opit).c_str(), ">")) {
						retcount = bpt->rangeSearch((unsigned char*) (maxchar),
								NULL, &indresult, EXCLUDE, EXCLUDE);

					} else if (!strcmp((*opit).c_str(), ">=")) {
						retcount = bpt->rangeSearch((unsigned char*) (maxchar),
								NULL, &indresult, INCLUDE, INCLUDE);

					} else if (!strcmp((*opit).c_str(), "==")) {

						retcount = bpt->rangeSearch((unsigned char*) (maxchar),
								NULL, &indresult, EQUAL, EQUAL);

					}
				}
					break;
				}//end of switch

				//					cout << "retcount " << retcount << endl;
				vector<BPlusTree::searchresultstruct>::iterator bit;
				int len;
				for (bit = indresult.begin(); bit != indresult.end(); ++bit) {

					record rec;
					rec.recLen = len;
					heap->getRecord(st.tableDP, (*bit).record, rec.rec, len);
					rec.recLen = len;
					rec.rid = (*bit).record;
					//cout << rec.rid.pageno << "\t";
					resultset.push_back(rec);

				}
			} else {
				isOpenScan = true;
				resultset.clear();
				break;
			}
		}

	}

	if (isOpenScan == true) {
		resultset.clear();
		heap->openScan(st.tableDP, resultset);
	}
	//int cnt=0;
	for (it1 = resultset.begin(); it1 != resultset.end(); ++it1) {
		Status stu = ok;
		bool truth = 1;
		if (condition.size() != 0) {

			truth = isRecord(*it1, st.tableDP, condition, l, op, r, stu);

		}
		if (stu == ok && truth) {
			cnt++;
			updateRecord(*it1, columnlist, st.tableDP);
			heap->updateRecord(st.tableDP, (*it1).rid, (*it1).rec,
					(*it1).recLen);
			//readRecord(*it1, col_struct_list);
		}
		//where condition due
	}

	cout << "\nNos Record::" << cnt << "\nTime Taken::" << timer.unTapTime();

}

Status insertColoumn(int type, string value, int*&pos, int& offset,
		unsigned char* &record, syscols &s, int& actualSize) {

	Status status = ok;
	string temp;
	int t;
	int i, j;
	int* iint;
	long* lng;
	float* flot;
	unsigned char* str;
	unsigned char* date;
	unsigned char* varchar;
	unsigned char* time;
	switch (type) {
	t = *(int*) &record[*pos];
case 0:
	iint = (int*) &record[offset];// innt[4];
	*iint = atoi(value.c_str());
	*pos = offset;
	actualSize += INTEGER_SIZE * sizeof(char);
	offset += INTEGER_SIZE * sizeof(char);
	break;
case 1:
	lng = (long*) &record[offset];
	*lng = atol(value.c_str());
	*pos = offset;
	actualSize += LONG_SIZE * sizeof(char);
	offset += LONG_SIZE * sizeof(char);
	break;
case 2:
	flot = (float*) &record[offset];
	*flot = atof(value.c_str());
	*pos = offset;
	actualSize += FLOAT_SIZE * sizeof(char);
	offset += FLOAT_SIZE * sizeof(char);
	break;
case 3:
	str = &record[offset];
	if (strlen(value.c_str()) <= s.char_size) {
		for (i = 0; i < strlen(value.c_str()); i++) {

			str[i] = value.c_str()[i];
		}
		for (j = i; j < s.char_size; j++)
			str[j] = '\0';
	} else {
		cout << "\ntable name is too Long should be less than" << s.char_size;
		return error;
	}
	*pos = offset;
	actualSize += s.char_size * sizeof(char);
	offset += s.char_size * sizeof(char);
	break;
case 4:
	date = &record[offset];
	if (strlen(value.c_str()) == DATE_SIZE) {
		for (i = 0; i < DATE_SIZE; i++) {
			date[i] = value.c_str()[i];
		}

	} else {
		cout << "\nDate Size should be equal to" << DATE_SIZE;
		return error;
	}
	*pos = offset;
	actualSize += DATE_SIZE * sizeof(char);
	offset += DATE_SIZE * sizeof(char);
	break;
case 5:
	varchar = &record[offset];

	if (strlen(value.c_str()) <= s.char_size) {
		for (i = 0; i < strlen(value.c_str()); i++) {
			varchar[i] = value.c_str()[i];
		}
	} else {
		cout << "\nVarchar is too Long should be less than" << s.char_size;
		return error;
	}
	*pos = offset;
	actualSize += strlen(value.c_str()) * sizeof(char);
	offset += strlen(value.c_str()) * sizeof(char);
	break;
case 6:
	time = &record[offset];
	if (strlen(value.c_str()) == TIME_SIZE) {
		for (i = 0; i < TIME_SIZE; i++) {

			time[i] = value.c_str()[i];
		}

	} else {
		cout << "\nTIME  should be equal to" << TIME_SIZE;
		return error;
	}
	*pos = offset;
	actualSize += TIME_SIZE * sizeof(char);
	offset += TIME_SIZE * sizeof(char);
	break;
	}//end of switch
	return ok;
}

void _insert(string table, vector<string> columns, vector<string> values) {

	Status s;
	s = ok;
	string a;
	vector<te> all_col_struct_map;
	map<string, string> col_value_map;

	vector<string>::iterator it;
	vector<te>::iterator itm;
	map<string, string>::iterator itm1;

	systable st = syst(table);//pass table name
	if (st.tableDP == -1) { //not a valid Table
		cout << "\nTable not Found::" << table;
		return;
	}
	string temp;
	int size = 0;//max size of record
	int actualSize = 0;//size of record
	int i = 0;

	//make key value map of column and its values
	for (it = columns.begin(); it != columns.end(); ++it, ++i)
		col_value_map[*it] = values[i];

	//calculate record size
	all_col_struct_map = sysc_all(st.tableDP);
	for (itm = all_col_struct_map.begin(); itm != all_col_struct_map.end(); ++itm) {

		switch ((*itm).s.type) {
		case 0:
			size += INTEGER_SIZE * sizeof(char);
			break;
		case 1:
			size += LONG_SIZE * sizeof(char);
			break;
		case 2:
			size += FLOAT_SIZE * sizeof(char);
			break;
		case 3:
			size += MAX_STRING * sizeof(char);
			break;
		case 4:
			size += DATE_SIZE * sizeof(char);
			break;
		case 5:
			size += MAX_VARCHAR * sizeof(char);
			break;
		case 6:
			size += TIME_SIZE * sizeof(char);
			break;
		}//end of switch
	}

	size = size + (all_col_struct_map.size() * sizeof(int));//adding size of holding positions
	actualSize = actualSize + (all_col_struct_map.size() * sizeof(int));
	int offset = all_col_struct_map.size() * sizeof(int);
	unsigned char* record = (unsigned char*) malloc(size);

	for (itm = all_col_struct_map.begin(); itm != all_col_struct_map.end(); ++itm) {
		int *pos = (int*) &record[((*itm).s.pposition) * sizeof(int)];
		int type = (*itm).s.type;

		itm1 = col_value_map.find((*itm).colname);

		if (itm1 != col_value_map.end()) { //if found

			Status s = insertColoumn(type, col_value_map[(*itm).colname], pos,
					offset, record, (*itm).s, actualSize);
			if (s == error)
				return;
		}//end of if
		else {
			if ((*itm).s.isNULL == true) {
				if ((*itm).s.isdefault == true) {
					strcpy((char*) temp.c_str(), (char*) (*itm).s.colndefault);
					Status s = insertColoumn(type, temp, pos, offset, record,
							(*itm).s, actualSize);
					if (s == error)
						return;
				} else {
					*pos = offset;
				}

			} else {
				cout << (*itm).s.colnname << "Cannot Be null";
				return;
				//write nothing in record
			}
		}

	}//end of for
	rid r;
	heap->insertRecord(st.tableDP, record, actualSize, r);

	/////////////////////insert	index code start////////////////////

	//	cout << "\nRID::" << r.pageno << " " << r.slotno;
	vector<sysindex> si = sysi(st.tableDP);
	vector<sysindex>::iterator iti;
	for (iti = si.begin(); iti != si.end(); iti++) {
		//		(*iti)->
		//		(*iti)->noskeyattr

		unsigned char* rec = record;

		for (int i = 0; i < 1 /*(*iti).noskeyattr*/; i++) {//for every column
			int pos = (*iti).colms[i];
			int t;
			int length;
			unsigned char* iint;
			unsigned char* lng;
			unsigned char* flot;
			unsigned char* str;
			unsigned char* date;
			unsigned char* varchar;
			unsigned char* time;

			int nextpos, zeropos;

			cout << "\n";

			syscols it = sysc_pos(st.tableDP, pos);

			pos = (it.pposition) * sizeof(int);
			t = *((int*) &rec[pos]);//current pos

			nextpos = pos + sizeof(int);
			zeropos = *(int*) &rec[0];
			if (nextpos == zeropos) {
				length = actualSize - t;//next position - current
			} else {
				length = (*(int*) &rec[nextpos]) - t;//next position - current
			}
			if (length == 0) {
				cout << setw(SPACE) << left << "NULL";

			} else {
				BPlusTree* bt;

				string temp;
				temp.clear();
				switch (it.type) {
				case INTEGER: {
					int temp = *((int*) &record[t]);// innt[4];
					iint = (unsigned char*) &temp;
					bt = new BPlusTree(-1, (*iti).keyformat, (*iti).noskeyattr,
							(*iti).roothead);
					bt->insert(iint, r);
					break;
				}
				case LONG: {

					long temp = *((long*) &record[t]);// innt[4];
					lng = (unsigned char*) &temp;
					bt = new BPlusTree(-1, (*iti).keyformat, (*iti).noskeyattr,
							(*iti).roothead);
					bt->insert(lng, r);
					break;
				}
				case FLOAT: {
					float temp = *((float*) &record[t]);// innt[4];
					flot = (unsigned char*) &temp;
					bt = new BPlusTree(-1, (*iti).keyformat, (*iti).noskeyattr,
							(*iti).roothead);
					bt->insert(flot, r);
					break;
				}
				case STRING: {

					str = &rec[t];

					unsigned char maxchar[it.char_size];
					for (int i = 0; i < it.char_size; i++) {
						if (str[i] != '\0')
							maxchar[i] = str[i];
						else
							maxchar[i] = '\0';
					}
					bt = new BPlusTree(-1, (*iti).keyformat, (*iti).noskeyattr,
							(*iti).roothead);
					bt->insert(maxchar, r);
					break;
				}
				case VARCHAR: {

					varchar = &rec[t];

					unsigned char maxchar[it.char_size];
					for (int i = 0; i < it.char_size; i++) {
						if (varchar[i] != '\0')
							maxchar[i] = varchar[i];
						else
							maxchar[i] = '\0';
					}
					bt = new BPlusTree(-1, (*iti).keyformat, (*iti).noskeyattr,
							(*iti).roothead);
					bt->insert(maxchar, r);
					break;
				}
				}//end of switch


			}

		}

	}

}
void deleteRecord(record &r,vector<string> &l, int tableDP) ;
void _delete(string table, vector<int> condition, vector<string> l, vector<
		string> op, vector<string> r) {
	Timer timer;
	int cnt = 0;
	timer.tapTime();
	vector<int>::iterator il;
	vector<string>::iterator a, b, c;

	cout.flush();

	systable st;
	vector<syscols> col_struct_list;
	st = syst(table);
	if (st.tableDP == -1) { //not a valid Table
		cout << "\nTable not Found::" << table;
		return;
	}
	string temp;
	//		vector<string>::iterator it;
	//		for (it = columnlist.begin(); it != columnlist.end(); ++it) {
	//			temp = (*it).first;
	//			syscols sc;
	//			sc = sysc(st.tableDP, temp);
	//			if (sc.tableDP == -1) { //not a valid column
	//				cout << "\nColumn not Found::" << temp;
	//				return;
	//			}
	//			col_struct_list.push_back(sc);
	//
	//		}

	vector<record> resultset;

	vector<record>::iterator it1;
	bool isOpenScan = true;
	if (condition.size() == 1) {//look for index
		isOpenScan = false;
		vector<string>::iterator lit;
		vector<string>::iterator rit;
		vector<string>::iterator opit;
		vector<sysindex> sysind;
		sysind = sysi(st.tableDP);

		for (lit = l.begin(), opit = op.begin(), rit = r.begin(); lit
				!= l.end(), opit != op.end(), rit != r.end(); ++lit, ++rit, ++opit) {
			syscols litsyscol = sysc(st.tableDP, *lit);
			sysindex colindx = sysi_on_col(sysind, litsyscol.pposition);

			/////////////index is there///////////////////

			if (colindx.tableDP != -1 && strcmp((*opit).c_str(), "!=")) {
				vector<BPlusTree::searchresultstruct> indresult;
				BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
						colindx.noskeyattr, colindx.roothead);
				//					cout << "search value " << (*rit) << endl;

				int retcount;
				int dd0;
				long dd1;
				float dd2;

				switch (litsyscol.type) {
				case INTEGER: {
					dd0 = atoi((*rit).c_str());

					if (!strcmp((*opit).c_str(), "<")) {

						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd0), &indresult, EXCLUDE,
								EXCLUDE);

					} else if (!strcmp((*opit).c_str(), "<=")) {
						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd0), &indresult, INCLUDE,
								INCLUDE);

					} else if (!strcmp((*opit).c_str(), ">")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd0),
								NULL, &indresult, EXCLUDE, EXCLUDE);

					} else if (!strcmp((*opit).c_str(), ">=")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd0),
								NULL, &indresult, INCLUDE, INCLUDE);

					} else if (!strcmp((*opit).c_str(), "==")) {

						retcount = bpt->rangeSearch((unsigned char*) (&dd0),
								NULL, &indresult, EQUAL, EQUAL);

					}

					break;
				}
				case LONG:
					dd1 = atol((*rit).c_str());
					if (!strcmp((*opit).c_str(), "<")) {

						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd1), &indresult, EXCLUDE,
								EXCLUDE);

					} else if (!strcmp((*opit).c_str(), "<=")) {
						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd1), &indresult, INCLUDE,
								INCLUDE);

					} else if (!strcmp((*opit).c_str(), ">")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd1),
								NULL, &indresult, EXCLUDE, EXCLUDE);

					} else if (!strcmp((*opit).c_str(), ">=")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd1),
								NULL, &indresult, INCLUDE, INCLUDE);

					} else if (!strcmp((*opit).c_str(), "==")) {

						retcount = bpt->rangeSearch((unsigned char*) (&dd1),
								NULL, &indresult, EQUAL, EQUAL);

					}

					break;
				case FLOAT:
					dd2 = atof((*rit).c_str());
					if (!strcmp((*opit).c_str(), "<")) {

						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd2), &indresult, EXCLUDE,
								EXCLUDE);

					} else if (!strcmp((*opit).c_str(), "<=")) {
						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (&dd2), &indresult, INCLUDE,
								INCLUDE);

					} else if (!strcmp((*opit).c_str(), ">")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd2),
								NULL, &indresult, EXCLUDE, EXCLUDE);

					} else if (!strcmp((*opit).c_str(), ">=")) {
						retcount = bpt->rangeSearch((unsigned char*) (&dd2),
								NULL, &indresult, INCLUDE, INCLUDE);

					} else if (!strcmp((*opit).c_str(), "==")) {

						retcount = bpt->rangeSearch((unsigned char*) (&dd2),
								NULL, &indresult, EQUAL, EQUAL);

					}

					break;
				case STRING:
				case VARCHAR: {
					char maxchar[litsyscol.char_size];
					strcpy(maxchar, (*rit).c_str());
					for (int i = strlen((*rit).c_str()); i
							< litsyscol.char_size; i++) {
						maxchar[i] = '\0';
					}

					if (!strcmp((*opit).c_str(), "<")) {

						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (maxchar), &indresult,
								EXCLUDE, EXCLUDE);

					} else if (!strcmp((*opit).c_str(), "<=")) {
						retcount = bpt->rangeSearch(NULL,
								(unsigned char*) (maxchar), &indresult,
								INCLUDE, INCLUDE);

					} else if (!strcmp((*opit).c_str(), ">")) {
						retcount = bpt->rangeSearch((unsigned char*) (maxchar),
								NULL, &indresult, EXCLUDE, EXCLUDE);

					} else if (!strcmp((*opit).c_str(), ">=")) {
						retcount = bpt->rangeSearch((unsigned char*) (maxchar),
								NULL, &indresult, INCLUDE, INCLUDE);

					} else if (!strcmp((*opit).c_str(), "==")) {

						retcount = bpt->rangeSearch((unsigned char*) (maxchar),
								NULL, &indresult, EQUAL, EQUAL);

					}
				}
					break;
				}//end of switch

				//					cout << "retcount " << retcount << endl;
				vector<BPlusTree::searchresultstruct>::iterator bit;
				int len;
				for (bit = indresult.begin(); bit != indresult.end(); ++bit) {

					record rec;
					rec.recLen = len;
					heap->getRecord(st.tableDP, (*bit).record, rec.rec, len);
					rec.recLen = len;
					rec.rid = (*bit).record;
					//cout << rec.rid.pageno << "\t";
					resultset.push_back(rec);

				}
			} else {
				isOpenScan = true;
				resultset.clear();
				break;
			}
		}

	}

	if (isOpenScan == true) {
		resultset.clear();
		heap->openScan(st.tableDP, resultset);
	}
	//int cnt=0;
	for (it1 = resultset.begin(); it1 != resultset.end(); ++it1) {
		Status stu = ok;
		bool truth = 1;
		if (condition.size() != 0) {

			truth = isRecord(*it1, st.tableDP, condition, l, op, r, stu);

		}
		if (stu == ok && truth) {
			cnt++;
			deleteRecord(*it1,l, st.tableDP);
			heap->deleteRecord(st.tableDP, (*it1).rid);
			//readRecord(*it1, col_struct_list);
		}
		//where condition due
	}

	cout << "\nNos Record::" << cnt << "\nTime Taken::" << timer.unTapTime();

}
void deleteRecord(record &r,vector<string> &l, int tableDP) {

	vector<sysindex> sysind = sysi(tableDP);
	vector<string>::iterator ti;
for(ti=l.begin();ti!=l.end();++ti){
	syscols it = sysc(tableDP, (*ti));
	sysindex colindx = sysi_on_col(sysind, it.pposition);
	if(colindx.tableDP!=-1){//index is on column


string temp;

int pos;
int nextpos;
int zeropos;
unsigned char* rec = r.rec;

	int t;
	int length;



			temp.clear();
			switch ((it).type) {
			case 0: {
				unsigned char* old;
				old = &rec[t];
				BPlusTree *bpt=new BPlusTree(-1,colindx.keyformat,colindx.noskeyattr,colindx.roothead);
					bpt->deleteEntry(old, r.rid);

				break;
			}
			case 1: {
				unsigned char* old;
				old = &rec[t];
									BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(old, r.rid);

				break;
			}
			case 2: {
				unsigned char* old;
									BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(old, r.rid);
				break;
			}
			case 3: {
				unsigned char* old;

				old = &rec[t];


					unsigned char oldmaxchar[it.char_size];

					for (int i = 0; i < it.char_size; i++) {
						if (old[i] != '\0')
							oldmaxchar[i] = old[i];
						else
							oldmaxchar[i] = '\0';
					}


					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(oldmaxchar, r.rid);







				break;
			}
			case 4: {
				unsigned char* old;


				old = &rec[t];

					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(old, r.rid);






				break;
			}
			case 5: {
				nextpos = pos + sizeof(int);
				zeropos = *(int*) &rec[0];
				if (nextpos == zeropos) {
					length = r.recLen - t;//next position - current
				} else {
					length = (*(int*) &rec[nextpos]) - t;//next position - current
				}

				unsigned char* old;


				old = &rec[t];



					unsigned char oldmaxchar[it.char_size];

					for (int i = 0; i < it.char_size; i++) {
						if (old[i] != '\0')
							oldmaxchar[i] = old[i];
						else
							oldmaxchar[i] = '\0';
					}


					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(oldmaxchar, r.rid);



				break;
			}
			case 6: {
				unsigned char* old;


				old = &rec[t];



					BPlusTree *bpt = new BPlusTree(-1, colindx.keyformat,
							colindx.noskeyattr, colindx.roothead);
					bpt->deleteEntry(old, r.rid);






				break;
			}

			}//end of switch




	}


}//end of for


}
